ViewGroup{TextView,...}.getMeasuredHeight gives wrong value is smaller than real height.

Posted by Dewr on Stack Overflow See other posts from Stack Overflow or by Dewr
Published on 2011-01-12T12:45:42Z Indexed on 2011/01/12 12:53 UTC
Read the original article Hit count: 233

Filed under:

ViewGroup(the ViewGroup is containing TextViews having long text except line-feed-character).getMeasuredHeight returns wrong value... that is smaller than real height.

how to get rid of this problem?

here is the java code: ;public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; }

    int totalHeight = 0;
    int count = listAdapter.getCount();
    for (int i = 0; i < count; i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

;

and here is the list_item_comments.xml:; {RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" } {RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="8dip" android:layout_marginRight="8dip" } {TextView android:id="@+id/list_item_comments_nick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" style="@style/WhiteText" /} {TextView android:id="@+id/list_item_comments_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textSize="16sp" style="@style/WhiteText" /} {TextView android:id="@+id/list_item_comments_content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/list_item_comments_nick" android:autoLink="all" android:textSize="24sp" style="@style/WhiteText" /} {/RelativeLayout} {/RelativeLayout} ;

© Stack Overflow or respective owner

Related posts about android